//
//  HD_LY_TipView.m
//  HDHuNanMuseum
//
//  Created by liuyi on 2017/9/5.
//  Copyright © 2017年 liuyi. All rights reserved.
//

#import "HD_LY_TipView.h"
#import "TipContentView.h"

@implementation HD_LY_TipView

- (instancetype)initWithAlertView:(NSString *)msg style:(LYAlertActionStyle)style {
    if (self = [self initWithFrame:CGRectZero]) {
        self.frame = kKeyWindow.bounds;
        if (style == LYAlertActionStyleSuccess) {
            TipContentView *alertV = [TipContentView createViewFromNib];
            alertV.tipImgV.image = [UIImage imageNamed:@"user_success_tip"];
            alertV.bgView.layer.cornerRadius = 12;
            alertV.bgView.layer.masksToBounds = YES;
            CGFloat width = ScreenWidth * 0.6;
            CGFloat textH = [msg heightForFont:[UIFont systemFontOfSize:15] width:width*0.8];
            CGFloat hight = textH + 135;
            alertV.frame = CGRectMake(0, 0, width, hight);
            alertV.center = self.center;
            alertV.msgL.text = msg;
            [self addSubview:alertV];
            _isShow = NO;
            _alertView = alertV;
        }else if (style == LYAlertActionStyleError) {
            TipContentView *alertV = [TipContentView createViewFromNib];
            alertV.tipImgV.image = [UIImage imageNamed:@"user_error_tip"];
            alertV.bgView.layer.cornerRadius = 12;
            alertV.bgView.layer.masksToBounds = YES;
            CGFloat width = ScreenWidth * 0.6;
            CGFloat textH = [msg heightForFont:[UIFont systemFontOfSize:15] width:width*0.8];
            CGFloat hight = textH + 135;
            alertV.frame = CGRectMake(0, 0, width, hight);
            alertV.center = self.center;
            alertV.msgL.text = msg;
            [self addSubview:alertV];
            _isShow = NO;
            _alertView = alertV;
        }else if (style == LYAlertActionStyleMsg) {
            TipContentView *alertV = [[[NSBundle mainBundle] loadNibNamed:@"TipContentView" owner:self options:nil] lastObject];
            alertV.bgView.layer.cornerRadius = 5;
            alertV.bgView.layer.masksToBounds = YES;
            CGFloat width = ScreenWidth * 0.8;
            CGFloat textH = [msg heightForFont:[UIFont systemFontOfSize:15] width:width*0.8];
            CGFloat hight = textH + 15;
            alertV.frame = CGRectMake(0, 0, width, hight);
            alertV.center = self.center;
            alertV.msgL.text = msg;
            [self addSubview:alertV];
            _isShow = NO;
            _alertView = alertV;
        }
    }
    return self;
}

+ (instancetype)alertViewWithMsg:(NSString *)msg style:(LYAlertActionStyle)style; {
    
    return [[self alloc] initWithAlertView:msg style:style];
}

- (void)show {
    if (_isShow) return;
    if (self.superview == nil) {
        [kKeyWindow addSubview:self];
    }
    self.alpha = 0;
    _alertView.transform = CGAffineTransformScale(_alertView.transform,0.1,0.1);
    [UIView animateWithDuration:0.3 animations:^{
        _alertView.transform = CGAffineTransformIdentity;
        self.alpha = 1;
        _isShow = YES;
    }];
}

- (void)hide {
    if (self.superview) {
        [UIView animateWithDuration:0.3 animations:^{
            _alertView.transform = CGAffineTransformScale(_alertView.transform,0.1,0.1);
            self.alpha = 0;
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
            _isShow = NO;
        }];
    }
}

@end
